<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateAddressAPIRequest;
use App\Http\Requests\API\UpdateAddressAPIRequest;
use App\Models\Address;
use App\Models\Province;
use App\Repositories\AddressRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class AddressController
 * @package App\Http\Controllers\API
 */

class AddressAPIController extends ApiBaseController
{
    /** @var  AddressRepository */
    private $addressRepository;
    protected $model_name = 'App\Models\Address';
    protected $model_load_array = [
    ];

    public function __construct(AddressRepository $addressRepo)
    {
        $this->addressRepository = $addressRepo;
    }

    public function authLimit()
    {
        return [
            ['user_id', '=', Auth::id()],
            ['status', '=',1]
        ];
    }

    public function destroy($id)
    {
        $model = $this->getModel();
        $model->status = 0;
        $model->save();
        return responseZK(1);
    }

    public function beforeStore($id, $data)
    {
        $user_id = Auth::id();
        $youbian = \request('youbian');
        $province = Province::where('youbian',$youbian)->where('level',3)->first();
        Address::where('id',$id)->update(['is_select'=>1,'province_id'=>$province->id]);
        Address::where('id',$id)->update(['user_id'=>$user_id, 'is_select'=>1]);
        return ['status'=>1,'data'=>Address::find($id)->toArray(), 'msg'=>'ok'];
    }

    public function beforeUpdate($id, $request)
    {
        $province = Province::where('youbian',$request['youbian'])->where('level',3)->first();
        $model = $this->getModel();
        $obj = $model->find($id);
        if($province)
            $obj->province_id = $province->id;
        $obj->save();
        return responseZK(1,$obj);
    }
}
